import ConfigMarks from "../config-marks.js";
import FactoryPolygons from "../factory/factory-polygons.js";
import FactoryMaterial from "../factory/factory-material.js";
import FactoryLabel from "../factory/factory-label.js";
import ToolData from "../tool/tool-data.js";
import LayerUi from "./layer-ui.js";
import factoryPoint from "../factory/factory-point.js";

let layer;
let map;
let layerAreaBorder;
let layerAreaHight;
let geoJsonLayer;
let layerAreaBorderTag = false;
let layerCityBorder;
let layerCityPolygon;
let layerAreaName;

function startAsync(fun) {
	setTimeout(fun, 1);
}

function initBorder() {
	if (layerAreaBorderTag) {
		return;
	}
	map.addLayer(layerAreaBorder);
	layerAreaBorderTag = true;
}

const layerArea = {
	init(mapParam) {
		map = mapParam;

		layer = new mars3d.layer.GraphicLayer({
			name: "区域样式图层",
			zIndex: 999
		});

		layerAreaHight = new mars3d.layer.GraphicLayer({
			name: "区域样式图层 - 高亮",
			zIndex: 1000
		});

		ToolData.queryChinaMapFullBorderData().then(data => {
			layerAreaBorder = new mars3d.layer.GeoJsonLayer({
				name: "省界线图层",
				allowDrillPick: false,
				data,
				symbol: {
					type: "polyline",
					styleOptions: {
						setHeight: ConfigMarks.baseHeight + 500,
						...FactoryPolygons.areaBorder.style(),
					}
				}
			});

			geoJsonLayer = new mars3d.layer.GeoJsonLayer({
				name: "省面图",
				data,
				opacity: 0,
				symbol: {
					type: "polygon",
					styleOptions: {
						outline: false,
						// extrudedHeight: ConfigMarks.baseHeight,
						// setHeight: ConfigMarks.baseHeight,
						...FactoryPolygons.transparent.style(),
					}
				}
			});

			// 在layer上绑定监听事件
			geoJsonLayer.on(mars3d.EventType.click, function(event) {
				// map.removeLayer(layerAreaBorder);
				if (map.basemap != 2017) {
					map.basemap = 2017;
					setTimeout(() => {
						LayerUi.hide();
					}, 3000);
				}
				layerArea.initArea(event.graphic.options.attr.adcode, undefined, event.graphic
					.options.positions);
			});

			// 在layer上绑定右键监听事件
			geoJsonLayer.on(mars3d.EventType.rightClick, function(event) {
				layerArea.clear();
				LayerUi.show();
				setTimeout(() => {
					map.basemap = 10000;
					map.setCameraView({
						...ConfigMarks.center
					});
					layerArea.initAreaName();
				}, 100);
				// layerArea.initArea();
				// map.addLayer(layerAreaBorder);

			});

			map.addLayer(geoJsonLayer);

		});

		map.addLayer(layer);
		map.addLayer(layerAreaHight);
		layerArea.initAreaName();
	},
	add(entity) {
		layer.addGraphic(entity);
	},
	clear() {
		layer.clear();
		map.removeLayer(layerCityBorder);
		map.removeLayer(layerCityPolygon);
		layerCityBorder = undefined;
		layerCityPolygon = undefined;
		layerArea.initAreaName(false);
	},
	async initAreaHight(areaCode = "100000", style = FactoryPolygons.polygons101.style(), positions) {

		layerAreaHight.clear();

		if (!positions) {
			let data = await ToolData.queryChinaMapBorderData(areaCode);
			positions = data.features[0].geometry.coordinates[0][0].map(item => ({
				lat: item[1],
				lng: item[0],
				alt: 99990000
			}));

		}

	},
	async initAreaName(show = true) {
		if (!layerAreaName) {
			// 创建矢量数据图层（业务数据图层）
			map.addLayer(layerAreaName = new mars3d.layer.GraphicLayer({
				name: "省名称图层",
				// 点的聚合配置
				// clustering: {
				// 	enabled: true,
				// 	pixelRange: 20,
				// 	clampToGround: false
				// }
			}));
			ToolData.queryChinaMapFullBorderData().then(data => {
				setTimeout(() => {
					for (var i = 0; i < data.features.length; i++) {
						let code = data.features[i].properties.adcode;
						let center = data.features[i].properties.center;
						let name = data.features[i].properties.name;
						let index = i;
						setTimeout(() => {
							console.log(index)
							if (index % 2 == 1) {
								layerAreaName.addGraphic(factoryPoint.pointLightCone(
									center));
							} else {
								layerAreaName.addGraphic(factoryPoint.pointAreaName(
									center, name));
							}
						}, 100 * index);
					}
				}, 5000);
			})
		}
		layerAreaName.setOptions({
			show
		});
	},
	async initArea(areaCode = "100000", style = FactoryPolygons.polygons101.style(), positions) {
		layerArea.clear();

		if (!positions) {
			let data = await ToolData.queryChinaMapBorderData(areaCode);
			positions = data.features[0].geometry.coordinates[0][0].map(item => ({
				lat: item[1],
				lng: item[0],
				alt: 99990000
			}));
		}
		ToolData.queryChinaMapFullBorderData(areaCode).then(dataFull => {
			layerCityBorder = new mars3d.layer.GeoJsonLayer({
				name: "市界线图层",
				allowDrillPick: false,
				data: dataFull,
				symbol: {
					type: "polyline",
					styleOptions: {
						setHeight: ConfigMarks.baseHeight + 10,
						...FactoryPolygons.areaBorder.style()
					}
				}
			})
			layerCityPolygon = new mars3d.layer.GeoJsonLayer({
				name: "市面图",
				data: dataFull,
				opacity: 1,
				symbol: {
					type: "polygon",
					styleOptions: {
						outline: false,
						extrudedHeight: ConfigMarks.baseHeight,
						setHeight: ConfigMarks.baseHeight,
						// ...style,
						...FactoryPolygons.transparent.style(),
						label: FactoryLabel.label1()
					}
				}
			})
			setTimeout(() => {
				map.addLayer(layerCityBorder);
				map.addLayer(layerCityPolygon);
			}, 4000);
		})

		setTimeout(() => {
			// 主体背景
			let entity;
			layer.addGraphic(entity = new mars3d.graphic.PolygonEntity({
				positions,
				flyTo: true,
				style: {
					outline: false,
					outlineWidth: 5,
					outlineColor: "#7ed1ff",
					extrudedHeight: ConfigMarks.baseHeight,
					setHeight: ConfigMarks.baseHeight,
					hasShadows: true,
					...style,
				}
			}))
			map.flyToGraphic(entity, {
				// heading: 335,
				// pitch: -53,
				heading: 12,
				pitch: -44,
				scale: 1.5
			});
		}, 100);

		setTimeout(() => {
			// // 底部白色遮挡层
			layer.addGraphic(new mars3d.graphic.PolygonEntity({
				positions,
				style: {
					extrudedHeight: ConfigMarks.baseHeight - 500,
					setHeight: ConfigMarks.baseHeight - 500,
					...FactoryPolygons.polygons100.style()
				}
			}))
		}, 200);

		setTimeout(() => {
			// 渐变面 - 模拟内发光(效果不理想)
			layer.addGraphic(new mars3d.graphic.PolygonEntity({
				positions,
				style: {
					extrudedHeight: ConfigMarks.baseHeight + 30,
					setHeight: ConfigMarks.baseHeight + 30,
					...FactoryPolygons.polygons113.style()
				}
			}))
		}, 500);

		setTimeout(() => {
			// 边缘 - 高光线
			layer.addGraphic(new mars3d.graphic.PolylineEntity({
				positions,
				style: {
					setHeight: ConfigMarks.baseHeight + 1000,
					...FactoryPolygons.polygons206.style()
					// ...FactoryPolygons.polygons101.style()
				}
			}))
		}, 1000);

		setTimeout(() => {
			// 面：高亮区域添加
			layer.addGraphic(new mars3d.graphic.PolygonEntity({
				positions,
				style: {
					extrudedHeight: ConfigMarks.baseHeight + 1,
					setHeight: ConfigMarks.baseHeight + 1,
					color: "#7cbaff",
					opacity: 0.1,
				}
			}))
		}, 1500);

		setTimeout(() => {
			// 墙体
			let wallEntity = new mars3d.graphic.WallEntity({
				positions,
				style: {
					setHeight: 0,
					diffHeight: ConfigMarks.baseHeight + 500, // 墙高
					...FactoryPolygons.polygons209.style(),
				}
			});
			layer.addGraphic(wallEntity);
		}, 1200);

		// initBorder();
	}
}

export default layerArea;
